function! FixSQL()
    %s///ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<declare\>\)/\=submatch(3)!="" ? "DECLARE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<varchar\>\)/\=submatch(3)!="" ? "VARCHAR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<datetime\>\)/\=submatch(3)!="" ? "DATETIME" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<float\>\)/\=submatch(3)!="" ? "FLOAT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<decimal\>\)/\=submatch(3)!="" ? "DECIMAL" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<int\>\)/\=submatch(3)!="" ? "INT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<set\>\)/\=submatch(3)!="" ? "SET" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<if\>\)/\=submatch(3)!="" ? "IF" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<select\>\)/\=submatch(3)!="" ? "SELECT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<update\>\)/\=submatch(3)!="" ? "UPDATE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<insert\>\)/\=submatch(3)!="" ? "INSERT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<delete\>\)/\=submatch(3)!="" ? "DELETE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<into\>\)/\=submatch(3)!="" ? "INTO" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<from\>\)/\=submatch(3)!="" ? "FROM" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<where\>\)/\=submatch(3)!="" ? "WHERE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<and\>\)/\=submatch(3)!="" ? "AND" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<is\>\)/\=submatch(3)!="" ? "IS" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<null\>\)/\=submatch(3)!="" ? "NULL" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<exec\>\)/\=submatch(3)!="" ? "EXEC" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<execute\>\)/\=submatch(3)!="" ? "EXECUTE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<values\>\)/\=submatch(3)!="" ? "VALUES" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(@@\<error\>\)/\=submatch(3)!="" ? "@@ERROR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(@@\<fetch_status\>\)/\=submatch(3)!="" ? "@@FETCH_STATUS" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<begin\>\)/\=submatch(3)!="" ? "BEGIN" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<return\>\)/\=submatch(3)!="" ? "RETURN" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<else\>\)/\=submatch(3)!="" ? "ELSE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<grant\>\)/\=submatch(3)!="" ? "GRANT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<go\>\)/\=submatch(3)!="" ? "GO" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<exists\>\)/\=submatch(3)!="" ? "EXISTS" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<drop\>\)/\=submatch(3)!="" ? "DROP" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<as\>\)/\=submatch(3)!="" ? "AS" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<create\>\)/\=submatch(3)!="" ? "CREATE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<drop\>\)/\=submatch(3)!="" ? "DROP" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<end\>\)/\=submatch(3)!="" ? "END" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<raiserror\>\)/\=submatch(3)!="" ? "RAISERROR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<sum\>\)/\=submatch(3)!="" ? "SUM" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<max\>\)/\=submatch(3)!="" ? "MAX" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<min\>\)/\=submatch(3)!="" ? "MIN" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<count\>\)/\=submatch(3)!="" ? "COUNT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<cast\>\)/\=submatch(3)!="" ? "CAST" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<output\>\)/\=submatch(3)!="" ? "OUTPUT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<in\>\)/\=submatch(3)!="" ? "IN" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<fetch\>\)/\=submatch(3)!="" ? "FETCH" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<next\>\)/\=submatch(3)!="" ? "NEXT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<close\>\)/\=submatch(3)!="" ? "CLOSE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<deallocate\>\)/\=submatch(3)!="" ? "DEALLOCATE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<print\>\)/\=submatch(3)!="" ? "PRINT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<while\>\)/\=submatch(3)!="" ? "WHILE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<public\>\)/\=submatch(3)!="" ? "PUBLIC" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<scroll\>\)/\=submatch(3)!="" ? "SCROLL" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<cursor\>\)/\=submatch(3)!="" ? "CURSOR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<for\>\)/\=submatch(3)!="" ? "FOR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<not\>\)/\=submatch(3)!="" ? "NOT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<datediff\>\)/\=submatch(3)!="" ? "DATEDIFF" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<day\>\)/\=submatch(3)!="" ? "DAY" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<month\>\)/\=submatch(3)!="" ? "MONTH" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<year\>\)/\=submatch(3)!="" ? "YEAR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<round\>\)/\=submatch(3)!="" ? "ROUND" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<dateadd\>\)/\=submatch(3)!="" ? "DATEADD" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<top\>\)/\=submatch(3)!="" ? "TOP" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<binary\>\)/\=submatch(3)!="" ? "BINARY" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<textptr\>\)/\=submatch(3)!="" ? "TEXTPTR" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<rollback\>\)/\=submatch(3)!="" ? "ROLLBACK" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<transaction\>\)/\=submatch(3)!="" ? "TRANSACTION" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<commit\>\)/\=submatch(3)!="" ? "COMMIT" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<open\>\)/\=submatch(3)!="" ? "OPEN" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<procedure\>\)/\=submatch(3)!="" ? "PROCEDURE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<getdate\>\)/\=submatch(3)!="" ? "GETDATE" : ""/ge
	%s/\c\('\|"\).\{-}\('\|"\)\zs\|\(\<order\>\)/\=submatch(3)!="" ? "ORDER" : ""/ge
	%s/@\(\w\+\)/@\L\1\E/ge
	" %s/\c[^\n\s]BEGIN/\rBEGIN/ge
	"%g/\cSET QUOTED_IDENTIFIER OFF.*\nGO/normal dddd
	"%g/\cSET ANSI_NULLS ON.*\nGO/normal dddd
	"%g/\cSET QUOTED_IDENTIFIER ON.*\nGO/normal dddd
	"%g/\cSET ANSI_NULLS OFF.*\nGO/normal dddd
	%g#\cif.\{-}begin#execute "normal /\\cbeginix"
	%g#\c\(^\s*\)\@<!\zsbegin#execute "normal /\\cbegini"
endfunction
command! FixSQL silent call FixSQL()
